﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.ViewManagement;
using Windows8Accelerator.Core.Xaml;
using SimpleRssReader.DataSources;
using SimpleRssReader.Model;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml;
using System.Collections;
using Windows8Accelerator.Core.Xaml.Helpers;
using Windows8Accelerator.Core.Xaml.ViewModels;

namespace SimpleRssReader.ViewModel
{
    public class HomeViewModel : ViewModelBase
    {
        public async Task LoadAsync()
        {
            try
            {
                var cachedBlogs = await DataSource.CachedInstance.RetrieveBlogsWithLatestPostsAsync(5);
                var blogs = cachedBlogs.Object;
                ListHelper.MergeLists(Blogs, blogs, (s) => s.Id);
            }
            catch { }
        }
     
        public static Tuple<int, int> ItemColumnAndRowSpanSelector(object item, Tuple<int, int> defaultSpan)
        {
            if (ApplicationView.Value != ApplicationViewState.Snapped)
            {
                if (item is Post)
                {
                    var post = item as Post;
                    if (post.Equals(post.Blog.Posts.FirstOrDefault()))
                        return new Tuple<int,int>(2,2);
                }
            }
            return defaultSpan;
        }

        private ObservableCollection<Blog> _Blogs = new ObservableCollection<Blog>();
        public ObservableCollection<Blog> Blogs
        {
            get { return this._Blogs; }
        }
    }
}
